package com.prothink.classroomdao.impl;

import com.prothink.classroomdao.ClassRoomDao;
import com.prothink.spring.entity.ClassRoomEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author mayanping
 * @title: ClassRoomDaoImpl
 * @projectName spring-last
 * @description: TODO
 * @date 2024/1/1908:57
 */

@Repository
public class ClassRoomDaoImpl implements ClassRoomDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(ClassRoomEntity classRoomEntity) {
        String sql = "insert into classroom (class_name, student_count, create_user,create_time,update_user,update_time) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql, classRoomEntity.getClassName(),
                classRoomEntity.getStudentCount(),
                classRoomEntity.getCreateUser(),
                classRoomEntity.getCreateTime(),
                classRoomEntity.getUpdateUser(),
                classRoomEntity.getUpdateTime());
    }

    @Override
    public void deleteById(int id) {
        String sql = "delete from classroom where id =?";
        jdbcTemplate.update(sql, id);
    }

    @Override
    public void update(ClassRoomEntity classRoomEntity) {
        String sql = "update classroom set class_name=?, student_count=?, update_user=?, update_time=? where id = ?";
        jdbcTemplate.update(sql, classRoomEntity.getClassName(), classRoomEntity.getStudentCount(),
                classRoomEntity.getUpdateUser(), classRoomEntity.getUpdateTime(), classRoomEntity.getId());
    }

    @Override
    public List<ClassRoomEntity> selectAll() {
        String sql = "select * from classroom";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ClassRoomEntity.class));
    }

    @Override
    public ClassRoomEntity selectById(int id) {
        String sql = "select * from classroom where id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ClassRoomEntity.class), id);
    }

    @Override
    public void updateStudentCountById(int classId, int studentCount) {
        String sql = "update classroom set student_count=? where id = ?";
        jdbcTemplate.update(sql, studentCount, classId);
    }
}

